package com.example.module_home

import android.view.View
import android.widget.Toast
import androidx.fragment.app.activityViewModels
import androidx.recyclerview.widget.GridLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.ToastUtils
import com.example.lib_base.path.RouterPath
import com.example.lib_base.view.BaseFragment
import com.example.lib_common.view.entity.BannerEntity
import com.example.lib_common.view.entity.ServiceEntity
import com.example.module_home.adapter.HomeBannerAdapter
import com.example.module_home.adapter.HomeNoticeAdapter
import com.example.module_home.adapter.HotServiceAdapter
import com.example.module_home.databinding.FragmentHomeBinding
import com.example.module_home.vm.NoticeViewModel
import com.youth.banner.indicator.CircleIndicator
import com.youth.banner.transformer.AlphaPageTransformer
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
@Route(path = RouterPath.PATH_HOME_PAGE)
class HomeFragment : BaseFragment<FragmentHomeBinding>() {

    lateinit var hotServiceAdapter:HotServiceAdapter
    val homeNoticeAdapter=HomeNoticeAdapter()
    private val noticeViewModel:NoticeViewModel by activityViewModels()

    private val keyWords = arrayListOf("热门公告","节日彩蛋","园区新闻")
    //动图服务页面
    val hotServiceData= mutableListOf(
        ServiceEntity(R.drawable.park_home, "", "车位管理", 0),
        ServiceEntity(R.drawable.visitor_home, "", "访客登记", 1),
        ServiceEntity(R.drawable.repair_manager,"", "物业报修", 2),
        ServiceEntity(R.drawable.sign_home,"", "考勤打卡", 3),
        ServiceEntity(R.drawable.house_home,"", "房源信息", 4),
        ServiceEntity(R.drawable.shop_home,"", "商家推荐", 5),
        ServiceEntity(R.drawable.member_home,"", "月卡会员", 6),
        ServiceEntity(R.drawable.device_home,"", "设备管理", 7),
    )
    override fun getLayoutId(): Int =R.layout.fragment_home

    private fun dispatchServicePage(service:ServiceEntity){
      var path= when(service.typeId){
            0-> "/app/ParkingActivity"
            1->""
            2->""
            3->""
            4->"/app/HouseActivity"
            5->""
            6->""
            7->"/app/DeviceActivity"
            else -> ""
        }
        ARouter.getInstance().build(path).navigation()

    }
    override fun initView() {
        binding.rv1.adapter=homeNoticeAdapter
        noticeViewModel.getListData(1,10)
        noticeViewModel.mSuccessLiveData.observe(this){
            homeNoticeAdapter.data.addAll(it.data)
            homeNoticeAdapter.notifyDataSetChanged()
        }
        noticeViewModel.mErrorLiveData.observe(viewLifecycleOwner){errorMag->
            Toast.makeText(requireContext(),errorMag,Toast.LENGTH_SHORT).show()
        }
        //轮播图
        val bannerData= mutableListOf(
            BannerEntity(0,R.drawable.banner_img,""),
            BannerEntity(1,R.drawable.banner_img1,""),
            BannerEntity(2,R.drawable.banner_img2,"")
        )
        binding.banner.run {
            setAdapter(HomeBannerAdapter(bannerData))
            setPageTransformer(AlphaPageTransformer())
            setIndicator(CircleIndicator(this@HomeFragment.requireContext()))
            addBannerLifecycleObserver(this@HomeFragment.viewLifecycleOwner)
        }





        //设置跳转
        hotServiceAdapter=HotServiceAdapter(hotServiceData).apply {
            setItemOnCilkListener(object : HotServiceAdapter.ItemOnClickListener{
                override fun onClick(v: View, position: Int) {
                    this@HomeFragment.dispatchServicePage(hotServiceData[position])
                }
            })
        }
        binding.hotServiceRv.adapter=hotServiceAdapter
        binding.hotServiceRv.layoutManager=GridLayoutManager(requireContext(),4)


        //设置热门搜索
        binding.etSearch.run {
            setDatas(keyWords)
            setItemOnClickListener { data, position ->
                ToastUtils.showLong("搜索")
            }
        }
    }




    override fun onDestroyView() {
        super.onDestroyView()
        binding==null
    }


}